#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int N = 210;
typedef pair<int, int> PII;

char map[N][N];
int n, r, c;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dist[N][N];

int bfs(PII start, PII end)
{
    queue<PII> q;
    memset(dist, -1, sizeof dist);
    dist[start.first][start.second] = 0;
    q.push(start);
    while (q.size())
    {
        PII temp = q.front();
        q.pop();
        int x = temp.first, y = temp.second;
        for (int i = 0; i < 4; i++)
        {
            int a = x + dx[i], b = y + dy[i];
            if (a >= 0 && a < r && b >= 0 && b < c && map[a][b] != '#' && dist[a][b] == -1)
            {
                dist[a][b] = dist[x][y] + 1;
                if (a == end.first && b == end.second)
                    return dist[a][b];
                q.push({a, b});
            }
        }
    }
    return dist[end.first][end.second];
}
int main()
{
    cin >> n;

    while (n--)
    {
        cin >> r >> c;
        string line;
        for (int i = 0; i < r; i++)
            cin >> map[i];
        PII start, end;
        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                if (map[i][j] == 'S')
                    start.first = i, start.second = j;
                else if (map[i][j] == 'E')
                    end.first = i, end.second = j;
        // for (int i = 0; i < r; i++){
        //     for (int j = 0; j < c; j++)
        //         cout<<map[i][j]<<" ";
        //     cout<<endl;
        // }
        // cout<<endl;
        // cout<<start.first<<' '<<start.second<<" "<<end.first<< " "<<end.second <<endl;
        int d = bfs(start, end);
        if (d == -1)
            cout << "oop!" << endl;
        else
            cout << d << endl;
    }
    return 0;
}